package org.new9uy.tetris.model;

import java.awt.Color;
import java.util.Random;

import org.new9uy.tetris.GameConstants;
import org.new9uy.tetris.model.tetrimino.*;

public class Next
{   
    private Tetrimino tetrimino;
    private Mino[][] miniMatrix;
    private Matrix matrix;
    private Random rand;

    public Next(Matrix matrix)
    {
        this.miniMatrix = new Mino[4][4];
        this.matrix = matrix;
        this.rand = new Random();
        this.tetrimino = createRandomTetrimino();

        //INIT Private "Mini-matrix"
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0; i < 4; i++)
            {
                miniMatrix[i][j] = new Mino(i, j);
            }
        }

        //Draw the first piece:
        drawTetrimino(tetrimino, true);
    }

    public Tetrimino previewNext()
    {
        return tetrimino;
    }

    public Tetrimino getNext()
    {
        Tetrimino next = tetrimino;
        tetrimino = createRandomTetrimino();

        redraw(next);
        return (next);
    }

    private void redraw(Tetrimino oldnext)
    {
        drawTetrimino(oldnext, false);
        drawTetrimino(tetrimino, true);
    }
    //draw == true  => Draws tetrimino
    //draw == false => Erases tetrimino

    private void drawTetrimino(Tetrimino tet, boolean draw)
    {
        Mino[] mino = tet.getTetrimino();
        for (int i = 0; i < 4; i++)
        {
            Color color = GameConstants.MINO_EMPTY_COLOR;
            if (draw)
            {
                color = tet.getColor();
            }

            int x = mino[i].getX() - 4,
                    y = mino[i].getY() - 18;
            miniMatrix[x][y].setColor(color);
        }
    }

    private Tetrimino createRandomTetrimino()
    {
        return createRandomTetrimino(0);
    }

    /*
     * @param xtralines = 0..3;
     */
    private Tetrimino createRandomTetrimino(int xtralines)
    {
        if (xtralines > 3 || xtralines < 0)
        {
            throw new IllegalArgumentException();
        }

        int i = rand.nextInt(8 + xtralines);
        switch (i)
        {
            case 0:
                return new O(matrix);
            case 1:
                return new I(matrix);
            case 2:
                return new L(matrix);
            case 3:
                return new J(matrix);
            case 4:
                return new S(matrix);
            case 5:
                return new Z(matrix);
            case 6:
                return new T(matrix);
            case 7:
                return new I(matrix);
            case 8:
                return new I(matrix);
            case 9:
                return new I(matrix);
            case 10:
                return new I(matrix);
            default:
                return null;
        }
    }

    public int getWidth()
    {
        return miniMatrix[0].length;
    }

    public int getHeight()
    {
        return miniMatrix.length;
    }

    public Mino getMino(int x, int y)
    {
        return miniMatrix[x][y];
    }
}
